package by.grsu.hdm.devices;

import java.util.Random;

import by.grsu.hdm.exceptions.InvalidFormatException;
import by.grsu.hdm.media.Audio;
import by.grsu.hdm.media.IMedia;
import by.grsu.hdm.media.Video;
import by.grsu.hdm.model.HomeDevice;
import by.grsu.hdm.model.INetworkAvailable;
import by.grsu.hdm.model.IUsbPort;
import by.grsu.hdm.model.IUsbSupport;
import by.grsu.hdm.model.UsbPort;

public class MediaCenter extends HomeDevice implements IUsbSupport, INetworkAvailable, IMedia {
	
	private long trafficIn;
	private long trafficOut;
	
	private UsbPort usbPort;
	private Audio song = new Audio();
	private Video film = new Video();
	
	protected MediaCenter() {
		defaultConfiguration();
	}
	
	public MediaCenter(String id) {
		super(id);
		
		defaultConfiguration();
	}
	
	protected void defaultConfiguration()
	{
		this.trafficIn = 0;
		this.trafficOut = 0;
		
		this.usbPort = null;
	}
	
	@Override
	public long getOutgoingTraffic() {
		return this.trafficOut;
	}
	
	@Override
	public long getIncomingTraffic() {
		return this.trafficIn;
	}

	@Override
	protected void afterTurningOn() {
		this.trafficIn = Math.abs(new Random().nextInt());
		this.trafficOut = Math.abs(new Random().nextInt());
	}

	@Override
	protected void beforeTurningOff() {
		defaultConfiguration();
	}

	@Override
	public IUsbPort getUsbPort() {
		if (this.usbPort == null) { 
			this.usbPort = new UsbPort("USB-" + getDeviceId());
		}
		
		return this.usbPort;
	}
	
	public String getSongName() {	
		return song.getName();
	}

	public void setSongName(String name) {
		this.song.setName(name);
	}

	public String getFilmName() {
		return film.getName();
	}

	public void setFilmName(String name) {
		this.film.setName(name);
	}
	
	
	@Override
	public void playAudio(Audio song) throws InvalidFormatException{
		if ("MP3".equals(song.getFormat()))
		{
			System.out.println("Now played "+song.getFormat()+" file with "+song.getPlayTime()+" time of play.");
		}
		else throw new InvalidFormatException();
	}
	
	@Override
	public void playVideo(Video film) throws InvalidFormatException{
		if ("MP4".equals(film.getFormat())) 
		{
			System.out.println("Now played "+film.getFormat()+" file with "+film.getPlayTime()+" time of play.");
		}
		else throw new InvalidFormatException();
	}
}
